define(['angularAMD'], function (angularAMD) {
    angularAMD.service('IIoT0109GanttSetting', ['$rootScope', '$filter', '$translate', 'config', '$timeout', 'MMWService', '$mdDialog',
        function ($rootScope, $filter, $translate, config, $timeout, $MMWService, $mdDialog) {
            var _headers = [];
            this.init = function (options, feedback) {
                function openDialog(settings) {
                    $mdDialog.dialog(
                        'module/moduleIIoT/program/IIoT01/IIoT0109/IIoT0109GanttSetting.tmp.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleIIoT/program/IIoT01/IIoT0109/IIoT0109GanttSetting.css',
                                settings: settings,
                                selectHeaders: function () {
                                    $rootScope.showSelect({
                                        label: 'name',
                                        code: 'code',
                                        selectCode: dialog.settings.headers,
                                        list: _headers,
                                        confirm: function (selectItem, radio_dialog) {
                                            radio_dialog.hide();
                                            dialog.settings.headers_display = selectItem.name;
                                            dialog.settings.headers = selectItem.code;
                                        }
                                    });
                                },
                                selectColumns: function () {
                                    $mdDialog.dialog('JSplugins/angular-material-lite/template/checkList.tmp.html', function (checkListDialog) {
                                        return {
                                            title: $filter('translate')('IIoT0109.ganttSetting.group_column'),
                                            beforeShown: function () {
                                                var checklist_position = '';
                                                if (!window.cordova)
                                                    checklist_position = '#column-selector-list .ps-scrollbar-content';
                                                else
                                                    checklist_position = '#column-selector-list';

                                                sortablejs.create($(checklist_position)[0], {
                                                    ghostClass: 'ghost',
                                                    filter: '.undragable',
                                                    //handle: '.mdl-check-list__icon',
                                                    onEnd: function (evt) {
                                                        $timeout(function () {
                                                            var newIndex = evt.newIndex;
                                                            var oldIndex = evt.oldIndex;
                                                            var chooseItem = checkListDialog.itemList.splice(oldIndex, 1)[0];
                                                            checkListDialog.itemList.splice(newIndex, 0, chooseItem);
                                                            checkListDialog.itemList.forEach(function (item, index) {
                                                                item.order = index;
                                                                //sheet.cssRules[(item.index*2)].style.order = item.order;
                                                            });
                                                        });
                                                    }
                                                });
                                            },
                                            order: 'order',
                                            label: 'name',
                                            code: 'column',
                                            back: function () {

                                                dialog.settings.ganttGroups = checkListDialog.itemList;
                                                dialog.settings.group_columns_display = getGroupsDisplay(dialog.settings.ganttGroups);

                                                checkListDialog.hide();
                                            },
                                            itemList: dialog.settings.ganttGroups,
                                            itemClick: function (item, event) {
                                                item.isSelect = !item.isSelect;
                                            },
                                            isConfirm: true,
                                            isDrag: true
                                        };
                                    });
                                },
                                selectDateType: function () {
                                    $rootScope.showSelect({
                                        label: 'name',
                                        code: 'column',
                                        selectCode: dialog.settings.datetype,
                                        list: options.settings.datetypes,
                                        confirm: function (selectItem, radio_dialog) {
                                            radio_dialog.hide();
                                            dialog.settings.datetype_display = selectItem.name;
                                            dialog.settings.datetype = selectItem.column;
                                        }
                                    });
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                confirm: function () {
                                    if (dialog.settings.refresh.time > 0) {
                                        dialog.settings.refresh.is_start = 'Y';
                                    }
                                    var _settings = dialog.settings;
                                    dialog.hide();
                                    if (typeof (feedback) == "function") {
                                        feedback(_settings);
                                    }
                                    // if(options.callback){
                                    //     options.callback(_settings);
                                    // }
                                }
                            };
                        }
                    );
                }
                function getGroupsDisplay(_ganttGroups) {
                    var _group_columns_display = '';
                    _ganttGroups.forEach(function (item, index) {
                        if (item.isSelect) {
                            if (_group_columns_display != '')
                                _group_columns_display += '>';

                            _group_columns_display += item.name;
                        }
                    });
                    return _group_columns_display;
                }

                (function () {
                    _headers = [
                        { name: $filter('translate')('IIoT0109.ganttSetting.month'), code: 'month' },
                        { name: $filter('translate')('IIoT0109.ganttSetting.week'), code: 'week' },
                        { name: $filter('translate')('IIoT0109.ganttSetting.day'), code: 'day' },
                        { name: $filter('translate')('IIoT0109.ganttSetting.hour'), code: 'hour' }
                    ];
                    options.settings.headers_display = $filter('filter')(_headers, { code: options.settings.headers })[0].name;
                    options.settings.group_columns_display = getGroupsDisplay(options.settings.ganttGroups);
                    options.settings.refresh = {
                        is_start: 'N', time: 0
                    };
                    var _datetype_temp = $filter('filter')(options.settings.datetypes, { column: options.settings.datetype });
                    if (_datetype_temp.length > 0) {
                        options.settings.datetype_display = _datetype_temp[0].name;
                        options.settings.datetype = _datetype_temp[0].code;
                    } else {
                        options.settings.datetype_display = options.settings.datetypes[0].name;
                        options.settings.datetype = options.settings.datetypes[0].code;
                    }

                    openDialog(options.settings);
                })();
            };
        }]);
});
